package jp.sourceforge.akjrcp.audio;



import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class WavAudioSettingComposite extends Composite{
	public static final String FRAMERATE="FRAMERATE";
	
	
	public static final String CHANNEL="CHANNEL";
	
	public static final int SAME_SOURCE=-1;
	private String preferenceKey;
	private IPreferenceStore preferenceStore;
	private CCombo frameRate;
	private CCombo channel;


	
	public String getPreferenceKey() {
		return preferenceKey;
	}
	public void setPreferenceKey(String preferenceKey) {
		this.preferenceKey = preferenceKey;
	}
	public IPreferenceStore getPreferenceStore() {
		return preferenceStore;
	}
	public void setPreferenceStore(IPreferenceStore store) {
		this.preferenceStore = store;
		//update UI
		if(preferenceStore.contains(preferenceKey+"."+FRAMERATE)){
			int fr=preferenceStore.getInt(preferenceKey+"."+FRAMERATE);
			setFrameRate(fr);
		}
		



if(preferenceStore.contains(preferenceKey+"."+CHANNEL)){
	int ch=preferenceStore.getInt(preferenceKey+"."+CHANNEL);
	
	setChannel(ch);
}
	}
	
	public class ValueChanged implements SelectionListener{

		public void widgetDefaultSelected(SelectionEvent e) {
			
		}

		public void widgetSelected(SelectionEvent e) {
			if(e.getSource()==frameRate){
				
				if(preferenceStore!=null){
					preferenceStore.setValue(preferenceKey+"."+FRAMERATE, toStoredValue(frameRate));
					
				}
			}else if(e.getSource()==channel){
				if(preferenceStore!=null){
					preferenceStore.setValue(preferenceKey+"."+CHANNEL, toStoredValue(channel));
				}
			}
			
		}
		
	}
	
	public void updateStoreValue(String value,CCombo combo){
		int s=combo.getItemCount();
		for (int i = 0; i < s; i++) {
			if(value.equals(combo.getItem(i))){
				combo.select(i);
				return;
			}
		}
		combo.select(0);
	}
	

	
	public void updateFramerate(int[] framerates){
		int oldbitrate=getFramerate();
		frameRate.removeAll();
		
		frameRate.add(Messages.getString("MP3AudioSettingComposite.same_source"));
		for (int i = 0; i < framerates.length; i++) {
			frameRate.add(""+framerates[i]);
		}
		
		updateStoreValue(""+oldbitrate,frameRate);
	}
	

	
	public int toStoredValue(CCombo combo){
		String value="";
		if(combo.getSelectionIndex()<=0){
			value="-1";
		}else{
			if(combo!=channel){
			value=combo.getItem(combo.getSelectionIndex());
			}else{
				value=""+combo.getSelectionIndex();	
			}
		}
		return Integer.parseInt(value);
	}
	
	public void setChannel(int ch){
		if(ch>0){
			channel.select(ch);
		}else{
			channel.select(0);
		}
	}
	public int getChannel(){
		if(channel.getSelectionIndex()==0){
			return SAME_SOURCE;
		}else{
			return channel.getSelectionIndex();//1 or 2
		}
	}
	

	
	public void setFrameRate(int framerate){
		updateStoreValue(""+framerate,frameRate);
	}
	

	
	public int getFramerate(){
		if(frameRate.getSelectionIndex()<=0){
			return SAME_SOURCE;
		}else{
			return Integer.parseInt(frameRate.getItem(frameRate.getSelectionIndex()));
		}
	}
	
	public void setEnabled(boolean bool){
		super.setEnabled(bool);
		frameRate.setEnabled(bool);
	
		channel.setEnabled(bool);
		
	}
	
	public WavAudioSettingComposite(Composite parent, int style,boolean horizontal,String preferenceKey) {
		super(parent, style);
		this.preferenceKey=preferenceKey;
		if(horizontal){
			this.setLayout(new RowLayout());
		}else{
			this.setLayout(new GridLayout(3,false));
		}

		

		
		Label label4=new Label(this,SWT.NULL);
		label4.setText(Messages.getString("MP3AudioSettingComposite.convertframerate"));
		frameRate = new CCombo(this,SWT.READ_ONLY);
		frameRate.add(Messages.getString("MP3AudioSettingComposite.same_source"));
		
		
		Label label41=new Label(this,SWT.NULL);
		label41.setText("hz");
		
		
		updateFramerate(MpegAudioFormatUtil.all_framerate);
		
		setFrameRate(44100);
		
	
		
		Label label5=new Label(this,SWT.NULL);
		label5.setText(Messages.getString("MP3AudioSettingComposite.convertchannel"));
		channel = new CCombo(this,SWT.READ_ONLY);
		channel.add(Messages.getString("MP3AudioSettingComposite.same_source"));
		channel.add(Messages.getString("monoral"));
		channel.add(Messages.getString("stereo"));
		
		channel.select(2);//stereo
		
		ValueChanged valueChanged=new ValueChanged();
	
		frameRate.addSelectionListener(valueChanged);
		channel.addSelectionListener(valueChanged);
		this.pack();
	}
	
	



}
